home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / snit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  3.8 KB  |  141 lines

  1. /*
  2.  *  $Id: snit.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  snit.c
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
  12.  *    The Regents of the University of California.  All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that: (1) source code distributions
  16.  * retain the above copyright notice and this paragraph in its entirety, (2)
  17.  * distributions including binary code include the above copyright notice and
  18.  * this paragraph in its entirety in the documentation or other materials
  19.  * provided with the distribution, and (3) all advertising materials mentioning
  20.  * features or use of this software display the following acknowledgement:
  21.  * ``This product includes software developed by the University of California,
  22.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  23.  * the University nor the names of its contributors may be used to endorse
  24.  * or promote products derived from this software without specific prior
  25.  * written permission.
  26.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  27.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  28.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  29.  *
  30.  * Modifications made to accommodate the new SunOS4.0 NIT facility by
  31.  * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
  32.  * This module now handles the STREAMS based NIT.
  33.  */
  34.  
  35. #if (HAVE_CONFIG_H)
  36. #include "../include/config.h"
  37. #endif
  38. #include "../include/libnet.h"
  39.  
  40. #include "../include/gnuc.h"
  41. #ifdef HAVE_OS_PROTO_H
  42. #include "../include/os-proto.h"
  43. #endif
  44.  
  45.  
  46. struct link_int *
  47. open_link_interface(char *device, char *ebuf)
  48. {
  49.     struct strioctl si;        /* struct for ioctl() */
  50.     struct ifreq ifr;       /* interface request struct */
  51.     static char dev[] = "/dev/nit";
  52.     register struct link_int *l;
  53.  
  54.     l = (struct link_int *)malloc(sizeof(*l));
  55.     if (l == NULL)
  56.     {
  57.         strcpy(ebuf, ll_strerror(errno));
  58.         return (NULL);
  59.     }
  60.  
  61.     memset(l, 0, sizeof(*l));
  62.  
  63.     l->fd  = open(dev, O_RDWR);
  64.     if (l->fd < 0)
  65.     {
  66.         sprintf(ebuf, "%s: %s", dev, ll_strerror(errno));
  67.         goto bad;
  68.     }
  69.  
  70.     /*
  71.      *  arrange to get discrete messages from the STREAM and use NIT_BUF
  72.      */
  73.     if (ioctl(l->fd, I_SRDOPT, (char *)RMSGD) < 0)
  74.     {
  75.         sprintf(ebuf, "I_SRDOPT: %s", ll_strerror(errno));
  76.         goto bad;
  77.     }
  78.     if (ioctl(l->fd, I_PUSH, "nbuf") < 0)
  79.     {
  80.         sprintf(ebuf, "push nbuf: %s", ll_strerror(errno));
  81.         goto bad;
  82.     }
  83.     /*
  84.      *  request the interface
  85.      */
  86.     strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  87.     ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
  88.     si.ic_cmd = NIOCBIND;
  89.     si.ic_len = sizeof(ifr);
  90.     si.ic_dp = (char *)𝔦
  91.     if (ioctl(l->fd, I_STR, (char *)&si) < 0)
  92.     {
  93.         sprintf(ebuf, "NIOCBIND: %s: %s", ifr.ifr_name, ll_strerror(errno));
  94.         goto bad;
  95.     }
  96.  
  97.     ioctl(l->fd, I_FLUSH, (char *)FLUSHR);
  98.     /*
  99.      * NIT supports only ethernets.
  100.      */
  101.     l->linktype = DLT_EN10MB;
  102.  
  103.     return (l);
  104. bad:
  105.     if (l->fd >= 0)
  106.     {
  107.         close(l->fd);
  108.     }
  109.     free(l);
  110.     return (NULL);
  111. }
  112.  
  113.  
  114. int
  115. close_link_interface(struct link_int *l)
  116. {
  117.     return (close(l->fd));
  118. }
  119.  
  120.  
  121. int
  122. write_link_layer(struct link_int *l, const u_char *device, const u_char *buf,
  123.             int len)
  124. {
  125.     int c;
  126.     struct sockaddr sa;
  127.  
  128.     memset(&sa, 0, sizeof(sa));
  129.     strncpy(sa.sa_data, device, sizeof(sa.sa_data));
  130.  
  131.     c = sendto(l->fd, buf, len, 0, &sa, sizeof(sa));
  132.     if (c != len)
  133.     {
  134. #if (__DEBUG)
  135.         fprintf(stderr, "write_link_layer: %d bytes written (%s)\n", c,
  136.             strerror(errno));
  137. #endif
  138.     }
  139.     return (c);
  140. }
  141.